<template>
<div class="header-wrapper" :style="{backgroundImage: 'url(' + headerBg + ')', paddingTop: paddingTop + 'px'}">
    <div class="header">
      <div class="search" ref="search" >
        <input type="text" :placeholder="searchPlaceHolder" v-model="searchValue" @change="change" @keyup.enter="submitSearch"/>
        <i class="fa fa-search" aria-hidden="true"  @click.stop="submitSearch"></i>
        <i class="fa fa-times-circle" aria-hidden="true" v-if="showSearchBtn" @click="removeSearch"></i>
      </div>
      <div class="button" ref="button">
        <slot name="button">
          <p @click="back">取消</p>
        </slot>
      </div>
    </div>
</div>
</template>

<script>
import headerBg from '@/assets/img/header_bg.png'
import { isIphoneX } from '@/assets/js/brower'

export default {
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    searchPlaceHolder: {
      type: String,
      default: '搜索...'
    }
  },
  data () {
    return {
      headerBg,
      paddingTop: 0,
      searchValue: '',
      showSearchBtn: false
    }
  },
  created () {
    if (isIphoneX()) {
      this.paddingTop = 34
    } else {
      this.paddingTop = 24
    }
  },
  mounted () {
    // this.$refs.search.style.width = window.innerWidth - this.$refs.button.clientWidth - 30 - 24 - 60 - 18 + 'px'
  },
  methods: {
    back () {
      window.history.length === 0 ? this.$router.replace('/') : this.$router.go(-1)
    },
    change () {
      this.$emit('change', this.searchValue)
    },
    removeSearch () {
      this.searchValue = ''
      this.$emit('removeSearch')
    },
    submitSearch () {
      this.$emit('submitSearch', this.searchValue)
    }
  },
  watch: {
    searchValue (newValue, oldValue) {
      if (newValue.length > 0) {
        this.showSearchBtn = true
      } else {
        this.showSearchBtn = false
      }
    },
    value (val) {
      this.searchValue = val
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/css/variable.styl'
.header-wrapper {
  background-size: cover;
  background-repeat: no-repeat;

  .header{
    position: relative;
    font-size: $font-size-title;
    color: #fff;
    height: 40px;
    overflow: hidden;
    padding: 0 15px;

    .search {
      height: 28px;
      margin-right: 70px;
      border: 1px solid #fff;
      border-radius: 20px;
      margin-top: 5px;
      padding-left: 40px;
      padding-right: 20px;
      position: relative;

      input {
        background: none;
        border: none;
        height: 28px;
        width: 100%;
        caret-color: #5195d9;
        font-size: 15px;
        color: #fff;
        &:focus {
          outline: none
        }
      }

      .fa-search {
        position: absolute;
        left: 13px;
        top: 4px;
        font-size: 21px;
      }

      .fa-times-circle {
        position: absolute;
        right: 8px;
        top: 2px;
        font-size: 18px;
        padding: 3px;
        color: $color-border;
      }
    }

    .button {
      padding 0 12px
      height: 40px;
      line-height: 40px;
      position: absolute;
      right: 10px;
      top: 0;
    }
  }
}

input::-webkit-input-placeholder { /* WebKit browsers */
    color: #efefef;
}
</style>
